<?php
/**
 * This file manages the actions coming from
 * the admin pages
 *
 * @author University of London Computer Centre
 * @version 1.1.0
 * @package ArchivePress
 * @subpackage pages
 * @global ArchivePress $ArchivePress
 * @global ArchivePressLog $ArchivePressLog
 */

global $ArchivePress, $ArchivePressLog;

// check if the request is a proper request
if( (count($_GET) == 0) && (count($_POST) == 0) ) {
    exit;
} if(array_key_exists(WP_ARCHIVEPRESS_PREFIX . "action", $_GET)) { // GET actions
    // message to display
    $message = "";

    // ACTION REQUEST
    if(array_key_exists(WP_ARCHIVEPRESS_PREFIX . "action", $_GET)) {
        // switch request
        switch($_GET[WP_ARCHIVEPRESS_PREFIX . "action"]) {
            case "deactivatefeed":      // deactivate feed
                // security check
                check_admin_referer(WP_ARCHIVEPRESS_PREFIX . 'activate_deactivate_postfeed_' . (int)$_GET["feed_id"]);

                // Do it
                $ArchivePress->setFeed((int)$_GET["feed_id"], "active", "no");
                $message = "&inactive=1&feed_id=".$_GET["feed_id"];
                break;
            case "activatefeed":        // activate feed
                // security check
                check_admin_referer(WP_ARCHIVEPRESS_PREFIX . 'activate_deactivate_postfeed_' . (int)$_GET["feed_id"]);

                // Do it
                $ArchivePress->setFeed((int)$_GET["feed_id"], "active", "yes");
                $message = "&active=1&feed_id=".$_GET["feed_id"];
                break;
            case "updatefeed":          // update feed
                // security check
                check_admin_referer(WP_ARCHIVEPRESS_PREFIX . 'update_postfeed_' . (int)$_GET["feed_id"]);

                // Do it
                $feed = $ArchivePress->getFeed((int)$_GET["feed_id"]);
                $res = $ArchivePress->fetchFeed($feed);
                $message = "&fetchedfeed=1&feed_id=".$_GET["feed_id"]."&feeds=".$res;
                break;
            case "updatefeedcomments":  // update comments for a feed
                // security check
                check_admin_referer(WP_ARCHIVEPRESS_PREFIX . 'updatefeedcomments_postfeed_' . (int)$_GET["feed_id"]);

                // Do it
                $res = $ArchivePress->fetchFeedComments((int)$_GET["feed_id"]);
                $message = "&fetchedcomments=1&feed_id=".$_GET["feed_id"]."&feeds=".$res;
                break;
            case "resetfeed":           // reset feed
                // security check
                check_admin_referer(WP_ARCHIVEPRESS_PREFIX . 'resetfeed_postfeed_' . (int)$_GET["feed_id"]);

                // Do it
                $ArchivePress->resetFeed((int)$_GET["feed_id"]);
                $message = "&resetfeed=1&feed_id=".$_GET["feed_id"];
                break;
            case "deletefeed":          // delete feed
                // security check
                check_admin_referer(WP_ARCHIVEPRESS_PREFIX . 'deletefeed_postfeed_' . (int)$_GET["feed_id"]);

                // Do it
                $ArchivePress->deleteFeed((int)$_GET["feed_id"]);
                $message = "&deletefeed=1&feed_id=".$_GET["feed_id"];
                break;

            case "deactivatecommentfeed":      // deactivate feed
                // security check
                check_admin_referer(WP_ARCHIVEPRESS_PREFIX . 'activate_deactivate_commentfeed_' . (int)$_GET["feed_id"]);

                // Do it
                $ArchivePress->setFeed((int)$_GET["feed_id"], "active", "no");
                $message = "&inactive=1&feed_id=".$_GET["feed_id"];
                break;
            case "activatecommentfeed":        // activate feed
                // security check
                check_admin_referer(WP_ARCHIVEPRESS_PREFIX . 'activate_deactivate_commentfeed_' . (int)$_GET["feed_id"]);

                // Do it
                $ArchivePress->setFeed((int)$_GET["feed_id"], "active", "yes");
                $message = "&active=1&feed_id=".$_GET["feed_id"];
                break;
            case "updatecommentfeed":          // update feed
                // security check
                check_admin_referer(WP_ARCHIVEPRESS_PREFIX . 'update_commentfeed_' . (int)$_GET["feed_id"]);

                // Do it
                $res = $ArchivePress->fetchFeedComments((int)$_GET["feed_id"], true, true);
                $message = "&fetchedfeed=1&feed_id=".$_GET["feed_id"]."&feeds=".$res;
                break;

            case "deletelog":          // delete log
                // security check
                check_admin_referer(WP_ARCHIVEPRESS_PREFIX . 'deletelog_' . (int)$_GET["log_id"]);

                // Do it
                $ArchivePressLog->deleteLog((int)$_GET["log_id"]);
                $message = "&deletelog=1&log_id=".$_GET["log_id"];
                break;

            case "logon":          // log on
                // security check
                check_admin_referer(WP_ARCHIVEPRESS_PREFIX . 'logon');

                // Do it
                $ArchivePressLog->startLogging();
                update_option(WP_ARCHIVEPRESS_PREFIX . "defLogStatus", ArchivePressLog::LOG_ON);
                $message = "&logon=1";
                break;

            case "logoff":          // log off
                // security check
                check_admin_referer(WP_ARCHIVEPRESS_PREFIX . 'logoff');

                // Do it
                $ArchivePressLog->stopLogging();
                update_option(WP_ARCHIVEPRESS_PREFIX . "defLogStatus", ArchivePressLog::LOG_OFF);
                $message = "&logoff=1";
                break;

            case "emptylog":          // empty log
                // security check
                check_admin_referer(WP_ARCHIVEPRESS_PREFIX . 'emptylog');

                // Do it
                $ArchivePressLog->emptyLog();
                $message = "&logempty=1";
                break;


            // FILTERS & BULK ACTIONS
            case "show_postfeeds":      // posts filters
                // security check
                check_admin_referer(WP_ARCHIVEPRESS_PREFIX . 'show_postfeeds');

                // CHECK FOR BULK ACTIONS
                if( isset($_GET["doaction"]) || isset($_GET["doaction2"])){
                    // manage actions
                    $action = $_GET["action"];
                    if($action == -1) {
                        $action = $_GET["action2"];
                    }
                    for($i = 0; $i < count($_GET["feed"]); $i++) {
                        switch($action) {
                           case "deactivatefeed":      // deactivate feed
                                $ArchivePress->setFeed($_GET["feed"][$i], "active", "no");
                                break;
                            case "activatefeed":        // activate feed
                                $ArchivePress->setFeed($_GET["feed"][$i], "active", "yes");
                                break;
                            case "resetfeed":           // reset feed
                                $ArchivePress->resetFeed($_GET["feed"][$i]);
                                break;
                            case "deletefeed":          // delete feed
                                $ArchivePress->deleteFeed((int)$_GET["feed_id"]);
                                break;
                        }
                    }
                    // message
                    switch($action) {
                       case "deactivatefeed":      // deactivate feed
                            $message = "&inactive=".count($_GET["feed"]);
                            break;
                        case "activatefeed":        // activate feed
                            $message = "&active=".count($_GET["feed"]);
                            break;
                        case "resetfeed":           // reset feed
                            $message = "&resetfeed=".count($_GET["feed"]);
                            break;
                        case "deletefeed":          // delete feed
                            $message = "&deletefeed=".count($_GET["feed"]);
                            break;
                    }

                }
                // redirect to the proper url
                $url = "";
                foreach($_GET as $key => $value) {
                    if( ($key != "_wpnonce") && ($key != "page") && ($key != "arpr_action") && ($key != "back") && ($key != "action")
                        && ($key != "feed") && ($key != "action2") && ($key != "doaction") && ($key != "doaction2") ){
                        $url .= "&".$key."=".urlencode($value);
                    }
                }

                wp_redirect(get_bloginfo( 'wpurl' ).'/wp-admin/admin.php?page=archivepress/pages/'.$_GET["back"].".php".$url.$message);
                exit;
                break;

            case "show_commentfeeds":      // comments filters
                // security check
                check_admin_referer(WP_ARCHIVEPRESS_PREFIX . 'show_commentfeeds');

                // CHECK FOR BULK ACTIONS
                if( isset($_GET["doaction"]) || isset($_GET["doaction2"])){
                    // manage actions
                    $action = $_GET["action"];
                    if($action == -1) {
                        $action = $_GET["action2"];
                    }
                    for($i = 0; $i < count($_GET["feed"]); $i++) {
                        switch($action) {
                           case "deactivatefeed":      // deactivate feed
                                $ArchivePress->setFeed($_GET["feed"][$i], "active", "no");
                                break;
                            case "activatefeed":        // activate feed
                                $ArchivePress->setFeed($_GET["feed"][$i], "active", "yes");
                                break;
                        }
                    }
                    // message
                    switch($action) {
                       case "deactivatefeed":      // deactivate feed
                            $message = "&inactive=".count($_GET["feed"]);
                            break;
                        case "activatefeed":        // activate feed
                            $message = "&active=".count($_GET["feed"]);
                            break;
                    }

                }
                // redirect to the proper url
                $url = "";
                foreach($_GET as $key => $value) {
                    if( ($key != "_wpnonce") && ($key != "page") && ($key != "arpr_action") && ($key != "back") && ($key != "action")
                        && ($key != "feed") && ($key != "action2") && ($key != "doaction") && ($key != "doaction2") ){
                        $url .= "&".$key."=".urlencode($value);
                    }
                }

                wp_redirect(get_bloginfo( 'wpurl' ).'/wp-admin/admin.php?page=archivepress/pages/'.$_GET["back"].".php".$url.$message);
                exit;
                break;

            case "show_logs":      // log filters
                // security check
                check_admin_referer(WP_ARCHIVEPRESS_PREFIX . 'show_logs');

                // CHECK FOR BULK ACTIONS
                if( isset($_GET["doaction"]) || isset($_GET["doaction2"])){
                    // manage actions
                    $action = $_GET["action"];
                    if($action == -1) {
                        $action = $_GET["action2"];
                    }
                    for($i = 0; $i < count($_GET["log"]); $i++) {
                        switch($action) {
                           case "deletelog":      // deactivate feed
                                $ArchivePressLog->deleteLog($_GET["log"][$i]);
                                break;
                        }
                    }
                    // message
                    switch($action) {
                       case "deletelog":      // delete logs
                            $message = "&deletelog=".count($_GET["log"]);
                            break;
                    }

                }
                // redirect to the proper url
                $url = "";
                foreach($_GET as $key => $value) {
                    if( ($key != "_wpnonce") && ($key != "page") && ($key != "arpr_action") && ($key != "back") && ($key != "action")
                        && ($key != "feed") && ($key != "action2") && ($key != "doaction") && ($key != "doaction2") ){
                        $url .= "&".$key."=".urlencode($value);
                    }
                }

                wp_redirect(get_bloginfo( 'wpurl' ).'/wp-admin/admin.php?page=archivepress/pages/'.$_GET["back"].".php".$url.$message);
                exit;
                break;

          case "show_authors":      // feed authors filters
                // security check
                check_admin_referer(WP_ARCHIVEPRESS_PREFIX . 'show_authors');

                // redirect to the proper url
                $url = "";
                foreach($_GET as $key => $value) {
                    if( ($key != "_wpnonce") && ($key != "page") && ($key != "arpr_action") && ($key != "back") && ($key != "action")
                        && ($key != "feed") && ($key != "action2") && ($key != "doaction") && ($key != "doaction2") ){
                        $url .= "&".$key."=".urlencode($value);
                    }
                }

                wp_redirect(get_bloginfo( 'wpurl' ).'/wp-admin/admin.php?page=archivepress/pages/'.$_GET["back"].".php".$url.$message);
                exit;
                break;


            default: exit;
        }

        // redirect to the proper url
        wp_redirect(get_bloginfo( 'wpurl' ).'/wp-admin/admin.php?page=archivepress/pages/'.$_GET["back"].".php".$message);

    }

} if(array_key_exists(WP_ARCHIVEPRESS_PREFIX . "action", $_POST)) { // POST actions
    // ACTION REQUEST
    // switch request
    switch($_POST[WP_ARCHIVEPRESS_PREFIX . "action"]) {
        case "editpost":      // edit post feed
            // security check
            check_admin_referer(WP_ARCHIVEPRESS_PREFIX . 'edit-post_feed_' . (int)$_POST["feed_id"]);

            // do it
            // name
            $ArchivePress->setFeed((int)$_POST["feed_id"], "name", $_POST[WP_ARCHIVEPRESS_PREFIX . 'feedname']);

            // active link
            $ArchivePress->setActiveLink((int)$_POST["feed_id"], (int)$_POST[WP_ARCHIVEPRESS_PREFIX . 'feedlink']);

            // interval
            $ArchivePress->setFeedOption((int)$_POST["feed_id"], "interval", (int)$_POST[WP_ARCHIVEPRESS_PREFIX . 'feedinterval']);

            // active
            if(isset($_POST[WP_ARCHIVEPRESS_PREFIX . 'feedactive'])) {
                $ArchivePress->setFeed((int)$_POST["feed_id"], "active", "yes");
            } else {
                $ArchivePress->setFeed((int)$_POST["feed_id"], "active", "no");
            }

            // comments
            if(isset($_POST[WP_ARCHIVEPRESS_PREFIX . 'feedcomments'])) {
                $ArchivePress->setFeedOption((int)$_POST["feed_id"], "comments", "yes");
            } else {
                $ArchivePress->setFeedOption((int)$_POST["feed_id"], "comments", "no");
            }

            $message = "&update=1&feed_id=".(int)$_POST["feed_id"];
            break;

        case "editcomment":      // edit post feed
            // security check
            check_admin_referer(WP_ARCHIVEPRESS_PREFIX . 'edit-comment_feed_' . (int)$_POST["feed_id"]);

            // do it
            // active link
            $ArchivePress->setActiveLink((int)$_POST["feed_id"], (int)$_POST[WP_ARCHIVEPRESS_PREFIX . 'feedlink']);

            // interval
            $ArchivePress->setFeedOption((int)$_POST["feed_id"], "interval", (int)$_POST[WP_ARCHIVEPRESS_PREFIX . 'feedinterval']);

            // active
            if(isset($_POST[WP_ARCHIVEPRESS_PREFIX . 'feedactive'])) {
                $ArchivePress->setFeed((int)$_POST["feed_id"], "active", "yes");
            } else {
                $ArchivePress->setFeed((int)$_POST["feed_id"], "active", "no");
            }

            $message = "&update=1&feed_id=".(int)$_POST["feed_id"];
            break;

        case "addfeed":      // add a feed
            // security check
            check_admin_referer(WP_ARCHIVEPRESS_PREFIX . 'addfeed');

            // do it
            $testURL = $_POST[WP_ARCHIVEPRESS_PREFIX . 'feedurl'];
            $testNAME = $_POST[WP_ARCHIVEPRESS_PREFIX . 'feedname'];

            $feedAdded = false;
            $lastSentence = "";
            // ADD NEW FEED PROCEDURE
            // check if is a feed url
            try {
                if($ArchivePress->isFeed($testURL)){
                    try {
                        $ArchivePress->addLastCheckedFeed($testNAME,true);
                        $feedAdded = true;
                        $lastSentence = "DISPLAY SUCCESS: feed successfully added";
                    } catch (Exception $e) {
                        $lastSentence = $e->getMessage();
                        $feedAdded = false;
                    }
                } else {
                    // try autodiscovery
                    $feeds = array();
                    try {
                        $feeds = FeedAutoDiscovery::getFeedLinks($testURL);
                        // If the feed was not already added
                        if($ArchivePress->isFeedNew($feeds)) {
                            // Add Feed
                            try {
                                $ArchivePress->addFeed($feeds,$testURL,$testNAME);
                                $feedAdded = true;
                                $lastSentence = "DISPLAY SUCCESS: feed successfully added";
                            } catch (Exception $e) {
                                $lastSentence = $e->getMessage();
                                $feedAdded = false;
                            }
                        } else {
                            $lastSentence = "DISPLAY ERROR: feed already added!";
                        }
                    } catch (Exception $e) {
                        $lastSentence = $e->getMessage();
                        $feedAdded = false;
                    }
                }
            } catch (Exception $e) {
                // page not found or other curl errors
                $lastSentence = $e->getMessage();
                $feedAdded = false;
            }

            $message = "&added=1";
            break;

        default:exit;
    }

    // redirect to the proper url
    wp_redirect(get_bloginfo( 'wpurl' ).'/wp-admin/admin.php?page=archivepress/pages/'.$_POST["back"].".php".$message);
}



?>